package Main;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class GetGoods{
	String url = "jdbc:mysql://localhost:3306/goods";
    String userName = "root";
    String password = "kamisama";
	String driverName1 = "com.mysql.jdbc.Driver";
    String strSql1 = "update good set num=num-1 where id = ?";
    Scanner sc = new Scanner(System.in);
    	Connection con = null;
    	java.sql.PreparedStatement pstmt = null;
    	ResultSet rs = null;
	public void Getgoods(int i){
		try {
			Frame1 d = new Frame1();
			Class.forName(driverName1);
	        con = (Connection) DriverManager.getConnection(url, userName, password); // 根据参数的插入数据
	        pstmt = con.prepareStatement(strSql1);
	        System.out.println("请输入您要购买商品id");
            pstmt.setFloat(1, i);
            pstmt.executeUpdate();
            System.out.println("购买成功");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (SQLException e){
			e.printStackTrace();
		}finally {
            if (rs != null)
                try {
                    rs.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            if (pstmt != null){
                try {
                    pstmt.close();// 关闭语句*/
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();// 关闭连接
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
	}
}
